package com.zh.controller;


import com.alibaba.fastjson.JSON;
import com.zh.dto.input.ValidStudentModel;
import com.zh.model.RestResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

@Slf4j
@RestController
@RequestMapping("/valid")
public class ValidController {


    @PostMapping("/student")
    public RestResponse<String> studentValid(@RequestBody @Valid ValidStudentModel param, BindingResult bindingResult){
        log.info("ValidController_studentValid:param:{}", JSON.toJSONString(param));
        // 验证返回是否错误
        if (bindingResult.hasErrors()){
            return RestResponse.fail("1001", bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        return RestResponse.ok("ok");
    }

    @PostMapping("/student1")
    public RestResponse<String> studentValid1(@RequestBody @Valid ValidStudentModel param){
        log.info("ValidController_studentValid:param:{}", JSON.toJSONString(param));
        return RestResponse.ok("ok");
    }
}
